package com.example.a2209_mianshi.ui.login

import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.get
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.example.a2209_mianshi.R
import com.example.a2209_mianshi.Router
import com.example.a2209_mianshi.base.BaseActivity
import com.example.a2209_mianshi.databinding.ActivityLoginBinding
import com.example.a2209_mianshi.flow_eventbus.event.LoginEvent
import com.example.a2209_mianshi.flow_eventbus.event.WxAttach2PhoneEvent
import com.example.a2209_mianshi.flow_eventbus.observe.observeEvent
import com.example.a2209_mianshi.flow_eventbus.post.postEvent
import com.example.a2209_mianshi.mmkv.AuthMMKV
import com.jczy.cyclone.ui.base.BaseToolbarActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class LoginActivity : BaseToolbarActivity<LoginViewModel,ActivityLoginBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_login
    private lateinit var mNavController: NavController
    private val mViewModel:LoginViewModel by viewModels()
    companion object {
        fun jump() {
//            ARouter.getInstance().build(Router.Ui.LoginActivity).withTransition(R.anim.bottom_in, R.anim.fab_out).navigation()
        }
    }

    override fun initView() {
        super.initView()
        mNavController = (supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment).navController
    }

    override fun initListener() {
        super.initListener()
        mToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.action) {
                mNavController.navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
            }
            true
        }
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.currentFragment.collect {
                    manageFragment(it)
                }
            }
        }

        observeEvent<WxAttach2PhoneEvent>(isSticky = true) {
//            mViewModel.wxOpenId = it.wxOpenId
//            mViewModel.gender = it.gender
//            mViewModel.nickName = it.nickName
//            mViewModel.headImg = it.headImg
            //跳转绑定手机号
            mNavController.navigate(R.id.action_phoneLoginFragment_to_bindPhoneFragment)
        }
    }
    private fun manageFragment(it: String) {
        title = ""
        mToolbar.menu.clear()
        when (it) {
            PhoneLoginFragment::class.simpleName -> {
                mToolbar.setNavigationIcon(R.mipmap.icon_exit)
                mToolbar.inflateMenu(R.menu.single_menu)
                mToolbar.menu[0].title = "密码登录"
                mToolbar.menu[0].icon = null
            }
            else -> {
                mToolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
            }

        }
    }
    fun handleLoginSuccess() {
        //发送登录成功消息
        postEvent(LoginEvent())
        finish()
    }
    class ClickAreaEvent(val ev: MotionEvent)

}